﻿using _12306helper.Common;
using _12306helper.Common.Net;
using _12306helper.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _12306helper.Service
{
    public class UserService
    {
        public static bool Login(string name, string pwd, string code)
        {
            HttpClient client = new HttpClient(CommonUtil.Cookies);
            IDictionary<string, string> param = new Dictionary<string, string>();
            param.Add("loginUserDTO.user_name", name);
            param.Add("randCode", code);
            param.Add("userDTO.password", pwd);
            /**
             *{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"loginCheck":"Y"},"messages":[],"validateMessages":{}}
             **/
            string result=client.Post("https://kyfw.12306.cn/otn/login/loginAysnSuggest", client.createParameter(param), "", "https://kyfw.12306.cn/otn/login/init");
            string data=HtmlUtil.GetContent(result, "loginCheck\":\"", "\"},");
            if ("Y".Equals(data))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static List<Passenger> GetPassengers()
        {
            List<Passenger> list = new List<Passenger>();
            HttpClient client = new HttpClient(CommonUtil.Cookies);
            string result=client.Post(@"https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs", "", "", @"https://kyfw.12306.cn/otn/confirmPassenger/initDc");
            JObject job=JObject.Parse(result);
            IList<JToken> datas=job["data"]["normal_passengers"].Children().ToList();
            foreach(JToken t in datas){
                Passenger pa=JsonConvert.DeserializeObject<Passenger>(t.ToString());
                list.Add(pa);
            }
            return list;
        }
    }
}
